/*
 * Created by JFormDesigner on Mon Aug 28 21:06:52 CST 2023
 */

package src;

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import javax.swing.*;

/**
 * @author Lenovo
 */
public class delPage extends JFrame {
    public static StudentData sd = new StudentData();

    public static Student st = new Student();
    public int index = -1;
    public delPage() {
        initComponents();
    }

    private void buttonBackMouseClicked(MouseEvent e) {
        this.dispose();
        new mainPage().setVisible(true);
    }

    public void clean(){

        this.textFieldName.setText("");
        this.textFieldPro.setText("");
        this.textFieldBorn.setText("");
        this.textFieldGrade.setText("");
        this.radioButtonMan.setSelected(false);
        this.radioButtonWomen.setSelected(false);

    }
    private void buttonfind(ActionEvent e) throws IOException {
        clean();
        this.labelError.setText("");
        sd.getFileDate();
        int i = sd.findData(sd.stdsdata, sd.capacity,this.textFieldNumber.getText());
        if(i == -1){
            clean();
            this.labelError.setText("查无此人，请检查学号输入是否正确！");

        }else {
            this.textFieldName.setText(sd.stdsdata[i].getName());
            String sex = sd.stdsdata[i].getSex();
            if (sex.equals("男")) {
                this.radioButtonMan.setSelected(true);
                this.radioButtonWomen.setSelected(false);
            }
            if (sex.equals("女")) {
                this.radioButtonMan.setSelected(false);
                this.radioButtonWomen.setSelected(true);

            }
            this.textFieldPro.setText(sd.stdsdata[i].getPro());
            this.textFieldGrade.setText(sd.stdsdata[i].getGrade());
            this.textFieldBorn.setText(sd.stdsdata[i].getBorn());
            //信息映射到各个框
            index = i;
            this.buttonDel.setEnabled(true);
        }
    }

    private void buttondel(ActionEvent e) throws IOException {
        if(index >= 0) {
            for (int i = index; i < sd.capacity - 1; i++) {
                sd.stdsdata[i] = sd.stdsdata[i + 1];
            }


            //获取下标
            //数组中删除该项
            sd.stdsdata[sd.capacity-1] = null;
            sd.capacity--;
            sd.setAllStudents(sd.stdsdata, sd.capacity);
            //重写文件
            clean();
            this.labelError.setText("删除成功！");
        }else{
            this.labelError.setText("找不到同学，检查后重试！");
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        panel1 = new JPanel();
        label1 = new JLabel();
        textFieldNumber = new JTextField();
        buttonFind = new JButton();
        label2 = new JLabel();
        label3 = new JLabel();
        label4 = new JLabel();
        label5 = new JLabel();
        label6 = new JLabel();
        textFieldName = new JTextField();
        textFieldPro = new JTextField();
        textFieldGrade = new JTextField();
        textFieldBorn = new JTextField();
        radioButtonMan = new JRadioButton();
        radioButtonWomen = new JRadioButton();
        buttonDel = new JButton();
        buttonBack = new JButton();
        labelError = new JLabel();

        //======== this ========
        setTitle("删除页面");
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== panel1 ========
        {
            panel1.setLayout(null);

            //---- label1 ----
            label1.setText("输入要删除的学号：");
            panel1.add(label1);
            label1.setBounds(25, 20, 145, 27);
            panel1.add(textFieldNumber);
            textFieldNumber.setBounds(160, 20, 195, textFieldNumber.getPreferredSize().height);

            //---- buttonFind ----
            buttonFind.setText("\u67e5\u627e");
            buttonFind.addActionListener(e -> {
                try {
                    buttonfind(e);
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            });
            panel1.add(buttonFind);
            buttonFind.setBounds(new Rectangle(new Point(360, 20), buttonFind.getPreferredSize()));

            //---- label2 ----
            label2.setText("\u59d3\u540d\uff1a");
            panel1.add(label2);
            label2.setBounds(new Rectangle(new Point(80, 65), label2.getPreferredSize()));

            //---- label3 ----
            label3.setText("\u6027\u522b\uff1a");
            panel1.add(label3);
            label3.setBounds(new Rectangle(new Point(80, 105), label3.getPreferredSize()));

            //---- label4 ----
            label4.setText("\u4e13\u4e1a\uff1a");
            panel1.add(label4);
            label4.setBounds(new Rectangle(new Point(80, 145), label4.getPreferredSize()));

            //---- label5 ----
            label5.setText("\u5e74\u7ea7\uff1a");
            panel1.add(label5);
            label5.setBounds(new Rectangle(new Point(80, 185), label5.getPreferredSize()));

            //---- label6 ----
            label6.setText("\u51fa\u751f\uff1a");
            panel1.add(label6);
            label6.setBounds(new Rectangle(new Point(80, 225), label6.getPreferredSize()));
            panel1.add(textFieldName);
            textFieldName.setBounds(125, 60, 310, textFieldName.getPreferredSize().height);
            panel1.add(textFieldPro);
            textFieldPro.setBounds(125, 140, 310, 30);
            panel1.add(textFieldGrade);
            textFieldGrade.setBounds(125, 180, 310, 30);
            panel1.add(textFieldBorn);
            textFieldBorn.setBounds(125, 220, 310, 30);

            //---- radioButtonMan ----
            radioButtonMan.setText("\u7537");
            panel1.add(radioButtonMan);
            radioButtonMan.setBounds(new Rectangle(new Point(140, 105), radioButtonMan.getPreferredSize()));

            //---- radioButtonWomen ----
            radioButtonWomen.setText("\u5973");
            panel1.add(radioButtonWomen);
            radioButtonWomen.setBounds(new Rectangle(new Point(215, 105), radioButtonWomen.getPreferredSize()));

            //---- buttonDel ----
            buttonDel.setText("\u786e\u5b9a\u5220\u9664");
            buttonDel.setEnabled(false);
            buttonDel.addActionListener(e -> {
                try {
                    buttondel(e);
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            });
            panel1.add(buttonDel);
            buttonDel.setBounds(200, 290, 90, buttonDel.getPreferredSize().height);

            //---- buttonBack ----
            buttonBack.setText("\u8fd4\u56de");
            buttonBack.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    buttonBackMouseClicked(e);
                }
            });
            panel1.add(buttonBack);
            buttonBack.setBounds(new Rectangle(new Point(320, 290), buttonBack.getPreferredSize()));
            panel1.add(labelError);
            labelError.setBounds(15, 260, 185, 30);

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel1.getComponentCount(); i++) {
                    Rectangle bounds = panel1.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel1.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel1.setMinimumSize(preferredSize);
                panel1.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel1);
        panel1.setBounds(5, 0, 465, 335);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());

        //---- buttonGroup1 ----
        var buttonGroup1 = new ButtonGroup();
        buttonGroup1.add(radioButtonMan);
        buttonGroup1.add(radioButtonWomen);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JPanel panel1;
    private JLabel label1;
    private JTextField textFieldNumber;
    private JButton buttonFind;
    private JLabel label2;
    private JLabel label3;
    private JLabel label4;
    private JLabel label5;
    private JLabel label6;
    private JTextField textFieldName;
    private JTextField textFieldPro;
    private JTextField textFieldGrade;
    private JTextField textFieldBorn;
    private JRadioButton radioButtonMan;
    private JRadioButton radioButtonWomen;
    private JButton buttonDel;
    private JButton buttonBack;
    private JLabel labelError;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
